import common from '@/utils/common.js'
// const App = getApp().globalData;
// 1. 异步读取 2.获取本地 

function request(obj, parames = {}, isLoading = true) {
	let data = {
		method: typeof obj == 'string' ? 'post' : (obj.method ? obj.method : 'post'),
		url: typeof obj == 'string' ? obj : obj.url,
	};

	let baseUrl = getApp().globalData.XcxData.domain;
	let merchant_id = getApp().globalData.XcxData.merchant_id;

	// isLoading && uni.$emit('showLoading')

	let postData = common.joinRequestParam({
		data: parames,
		needSign: parames.needSign === false ? false : true
	});

	getApp().globalData.logDebug && console.log(data['url'], postData);

	return new Promise((resolve, reject) => {
		uni.request({
			url: baseUrl + (data['url'].indexOf('/') === 0 ? data['url'] : ('/' + data['url'])),
			method: data['method'],
			header: {
				"content-type": "application/x-www-form-urlencoded"
			},
			data: postData,
			success(res) {
				// isLoading && uni.$emit('hideLoading')
				getApp().globalData.logDebug && console.log(data['url'], res.data)
				if (res.statusCode === 200) {
					if (res.data && res.data.status == -1) {
						login()
					} else {
						res.data.message = res.data.message ? res.data.message : res.data.msg;
						resolve(res.data);
					}
				} else {
					reject(res)
					if (parames.needErrorMsg === false) {
						return;
					}
					uni.showToast({
						icon: 'none',
						title: '接口数据异常,稍后再试'
					})
				}
			},
			fail(res) {
				// isLoading && uni.$emit('hideLoading')
				console.error('request fail', res)
				reject(res)
				if (parames.needErrorMsg === false) {
					return;
				}
				uni.showToast({
					icon: 'none',
					title: '接口数据异常,稍后再试'
				})
			}
		})
	})
}

function upload(data, showLoading = false) {
	return new Promise((resolve, reject) => {
		showLoading && uni.showLoading()
		let formData = data.formData ? data.formData : {};

		let postData = common.joinRequestParam({
			data: data.formData ? data.formData : {},
			needSign: data.needSign === false ? false : true,
			request_type: 'object'
		});
		let requestUrl = getApp().globalData.XcxData.domain + data.url;

		uni.uploadFile({
			url: requestUrl,
			filePath: data.path,
			name: data.name,
			//这里根据自己的实际情况改
			formData: postData,
			//这里是上传图片时一起上传的数据
			method: "POST",
			success: response => {
				showLoading && uni.hideLoading()
				let res = JSON.parse(response.data);
				console.log(res)
				if (res.status == 1) {
					resolve(res);
				} else {
					uni.hideLoading()
					uni.showToast({
						title: '上传失败',
						icon: 'none'
					});
					reject()
				}
			},
			fail: res => {
				showLoading && uni.hideLoading()
				uni.showToast({
					title: '上传失败',
					icon: 'none'
				});
				reject()
			},
		});

	})
}

function login() {
	uni.showModal({
		title: '登陆提示',
		content: '您还未登录，暂不能体验该功能，请先登录！',
		showCancel: true,
		cancelText: '暂不登录',
		cancelColor: '#999',
		confirmText: '去登录',
		confirmColor: '#ff5c6d',
		success: function(res) {
			if (res.confirm) {
				uni.navigateTo({
					// url: '/pages/my/my'
					url:'/pages/mer/tabbar/my/my'
				});
			}
		}
	});
}

module.exports = {
	request,
	upload,
}
